//
//  AppDelegate.m
//  tristana
//
//  Created by 李康 on 2021/1/5.
//  Copyright © 2021 李康. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabbarVC.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
#import "ViewController.h"
#import "TrisTanadManager.h"
#import "ViewController.h"
#import <JPush/JPUSHService.h>
#import <AdSupport/AdSupport.h>
#import <UserNotifications/UserNotifications.h>


#define kJPushAppKey @"139158c468b4c61ce1ec2fa1"
#define kJPushChannel  @"App Store"

@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = NO;
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self.window setRootViewController:[ViewController new]];
    [self.window makeKeyAndVisible];
    
    [TrisTanadManager showMainView];

//    MainTabbarVC *vc = [MainTabbarVC new];
//    self.window.rootViewController = vc;
    [self configJpush:launchOptions];

    return YES;
}


 -(void)configJpush:(NSDictionary *)launchOptions{
     JPUSHRegisterEntity * Entity = [[JPUSHRegisterEntity alloc] init];
     if (@available(iOS 12.0, *)) {
         Entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
     }
     [JPUSHService registerForRemoteNotificationConfig:Entity delegate:self];
     
     [JPUSHService setupWithOption:launchOptions appKey: kJPushAppKey
                           channel:kJPushChannel
                  apsForProduction:true
             advertisingIdentifier:nil];
     
 }

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    
    NSDictionary * userInfos = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfos];
    }
    completionHandler();
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    [JPUSHService registerDeviceToken:deviceToken];
}
 

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
    if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        
    }else{
        
    }
}



@end
